var OrderModel = function (order, mode) {
  BaseModel.call(this);
  var self = this;

  var fn = {
    initData: function (order, mode) {
      var fields = ['sort', 'factoryId', 'orderId', 'companyOrderId', 'customerId', 'customerOrderId', 'customer', 'chiCun',
        'orderCount', 'color', 'colorDescription', 'dutyStatus', 'quanImg', 'putOutStatus'
      ];
      if (mode !== 'match') {
        fields.pushArray(['purchaseStatus', 'inStatus']);
      }
      self.setDataFromObj(order, fields);

      self.jiaoDate = SysUtil.formatString2Date(order.jiaoDate, 'YY/MM/DD');
      self.xiaDanDate = SysUtil.formatString2Date(order.xiaDanDate, 'YY/MM/DD');

      var companyOrderId = order.companyOrderId;
      if (companyOrderId === null || companyOrderId.trim() === '') {
        self.showOrderId = order.orderId;
      } else {
        self.showOrderId = companyOrderId.trim();
      }

      var orderMaterialList = order.orderMaterialList;
      if (SysUtil.isEmpty(orderMaterialList)) {
        orderMaterialList = [];
      }
      self.orderMaterialList = orderMaterialList;

      if (mode === 'match') {
        var wpcbs = this.getMatchWpcb(order);
        self.wpcbs = wpcbs;
      } else {
        var purchaseStatus = order.purchaseStatus;
        self.purchaseStatusHtml = this.getStatusHtml(order, purchaseStatus);

        var inStatus = order.inStatus;
        self.inStatusHtml = this.getStatusHtml(order, inStatus);

        var wpcbObj = this.getOrderWpcb(order);
        self.wpcb = wpcbObj;
      }
    },

    getStatusHtml: function (order, status) {
      var sort = order.sort;
      if (sort < 0) {
        status = 1;
      }
      var list = [{
        text: "否",
        color: "#f56c6c"
      }, {
        text: "是",
        color: "#5daf34"
      }, {
        text: "部分",
        color: "#cf9236"
      }];

      var obj = list[status];
      if (obj === undefined) {
        return '';
      }
      return `<span style="color:${obj.color}">${obj.text}</span>`;
    },

    getMatchWpcb: function (order) {
      var wpcbList = order.wpcbList;
      var wpcbs = [];
      for (var i = 0; i < wpcbList.length; i++) {
        var wpcb = wpcbList[i];
        var maixStatus = wpcb.maixStatus;
        if (maixStatus === 0) {
          continue;
        }
        var status = i18n.getMsg(`querryOrder.orderMatchStatus.${maixStatus}`);
        var obj = {
          id: wpcb.id,
          maixStatus: maixStatus,
          colorList: wpcb.colorList,
          color: status.color,
          workshopName: wpcb.workshopName,
          text: status.text
        };
        wpcbs.push(obj);
      }

      return wpcbs;
    },

    getOrderWpcb: function (order) {
      var wpcbList = order.wpcbList;
      if (wpcbList === null) {
        wpcbList = [];
      }
      var wpcbObj = {};
      for (var i = 0; i < wpcbList.length; i++) {
        var wpcb = wpcbList[i];
        var isFinish = wpcb.isFinish;
        var obj = {
          workshopName: wpcb.workshopName,
          isFinish: isFinish,
          detailList: wpcb.workShopMaterialDetailList
        };

        var sort = order.sort;
        if (sort < 0 || isFinish === 1) {
          obj.canUpdate = false;
        } else {
          obj.canUpdate = true;
        }

        if (isFinish !== 0) {
          var count = _.get(wpcb, 'processList[0].qualityCount');
          if (count === undefined || count === null) {
            count = '';
          }
          obj.count = count;

          var processName = _.get(wpcb, 'processList[0].processName');
          if (processName === undefined || processName === null) {
            processName = '';
          }
          obj.processName = processName;

          var creationTime = _.get(wpcb, 'processList[0].creationTime');
          if (creationTime !== undefined) {
            obj.creationTime = SysUtil.formatString2Date(creationTime);
          }
        }
        obj.finishTime = SysUtil.formatString2Date(wpcb.finishTime);
        var yuJiTime = SysUtil.formatString2Date(wpcb.yuJiTime);
        obj.yuJiTime = yuJiTime;
        if (yuJiTime === '') {
          obj.yuJiTimeStr = wpcb.yuJiTime;
        } else {
          obj.yuJiTimeStr = moment(yuJiTime).format('YYYY-MM-DD');
        }

        var status = this.getArriveStatus(order, wpcb);
        obj.status = status;

        this.setFinishStatus(order, obj);

        obj.peopleSetTime = SysUtil.formatString2Date(wpcb.peopleSetTime, 'YY/MM/DD');
        wpcbObj[wpcb.id] = obj;
      }
      return wpcbObj;
    },

    getArriveStatus: function (order, wpcb) {
      var sort = order.sort;
      if (sort < 0) {
        return '已到齐';
      }
      var detailList = wpcb.workShopMaterialDetailList;
      if (detailList === null) {
        detailList = [];
      }

      if (detailList.length === 0) {
        return '未到齐';
      }

      var detailGroup = _.groupBy(detailList, 'inStatus');
      var keys = Object.keys(detailGroup);
      if (keys.length > 1) {
        return '部分到齐';
      }

      var status = keys[0];
      var statusList = ['未到齐', '已到齐', '部分到齐'];
      return statusList[status];
    },

    setFinishStatus: function (order, wpcb) {
      var status, statusText;
      if (order.sort < 0) {
        status = 1;
        statusText = "已完成";
      } else {
        status = wpcb.isFinish;
        if (status === 3){
          statusText = '已完成';
        } else if (status === 2) {
          status = 2;
          statusText = wpcb.processName;
        } else {
          var statusList = ["未生产", "已完成"];
          statusText = statusList[status];
        }
      }
      wpcb.finishStatus = status;
      wpcb.finishStatusText = statusText;
    }
  };

  this.getWorkShopDetail = function (workShop, field) {
    if (workShop === undefined) {
      if (field === undefined) {
        return null;
      }
      return '';
    }

    var wpcb = this.wpcb;
    var detail = wpcb[workShop.id];
    if (detail === undefined) {
      if (field === undefined) {
        return null;
      }
      return '';
    }

    if (field === undefined) {
      return detail;
    }
    var value = _.get(detail, field, '');
    return value;
  }

  this.getOrderIdObj = function () {
    var postData = {
      type: 0,
      dutyStatus: 0,
      orderId: this.orderId
    };

    var fields = ['timeType', 'deptId', 'workShop', 'customerId', 'customer', 'companyOrderId', 'factoryId',
      'color', 'coloDescription', 'model', 'arts'
    ];
    for (var i = 0; i < fields.length; i++) {
      var field = fields[i];
      postData[field] = '';
    }
    return postData;
  };

  fn.initData(order, mode);
};
